﻿using System.Windows;
using System.Threading;
using System.Threading.Tasks;

namespace FlyingEye
{
    public static class LauncherLoading
    {
        private static SplashScreen? _splashScreen;
        private static readonly ManualResetEventSlim _showEvent = new(false);
        private static readonly ManualResetEventSlim _closeEvent = new(false);
        private static Task? _loadingTask;

        static LauncherLoading()
        {
            StartLoadingThread();
        }

        private static void StartLoadingThread()
        {
            _loadingTask = Task.Run(() =>
            {
                // 创建和操作必须在同一STA线程
                _splashScreen = new SplashScreen("StartLoading.png");

                _showEvent.Wait();  // 等待显示信号
                _splashScreen.Show(false);

                _closeEvent.Wait(); // 等待关闭信号
                _splashScreen.Close(TimeSpan.Zero);

                // 立即释放资源
                _splashScreen = null;
                _showEvent.Dispose();
                _closeEvent.Dispose();
            });
        }

        public static void Show() => _showEvent.Set();
        public static void Close() => _closeEvent.Set();
    }
}